const webpack = require('webpack')
const path = require('path')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')

const vendor = [
  'react',
  'react-dom',
  'react-router',
  'redux',
  'react-redux'
]

module.exports = {
  devtool: 'source-map',
  target: 'web',
  context: path.join(__dirname, '../'),
  entry: {
    main: './src/index.js',
    vendor: vendor
  },
  output: {
    path: path.join(__dirname, '../dist'),
    publicPath: '',
    filename: 'js/[name]-[hash].js',
    chunkFilename: 'js/[name]-[hash].chunk.js'
  },
  module: {
    loaders: [{
      test: /\.js[x]?$/,
      exclude: /node_modules/,
      loader: 'babel',
      query: {
        presets: [
          'es2015',
          'stage-0',
          'react'
        ]
      }
    }, {
      test: /\.css$/,
      loader: ExtractTextPlugin.extract('style', 'css!postcss')
    }, {
      test: /\.less/,
      loader: ExtractTextPlugin.extract('style', 'css!less')
    }, {
      test: /\.scss$/,
      exclude: /node_modules/,
      loader: ExtractTextPlugin.extract('style', 'css?module&localIdentName=[hash:base64:5]!postcss!sass')
    }, {
      test: /\.(png|jpg|jpeg|gif)$/,
      loader: 'url-loader?limit=3072&name=images/[hash:6].[ext]'
    }, {
      test: /\.(woff|woff2|eot|ttf|svg)(\?.*$|$)/,
      loader: 'url'
    }]
  },
  resolve: {
    modulesDirectories: [
      path.join(__dirname, '../node_modules')
    ],
    extensions: ['', '.js', '.jsx']
  },
  plugins: [
    //new CopyWebpackPlugin([{
    //  context: './src/assets/img',
    //  from: '**/*',
    //  to: './assets/img'
    //}, {
    //  context: './src/assets/iconFont',
    //  from: '**/*',
    //  to: './assets/iconFont'
    //}]),
    new ExtractTextPlugin('css/[name]-[hash].css'),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor'
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false
      },
      output: {
        comments: false
      },
      sourceMap: true
    }),
    new webpack.optimize.DedupePlugin(),
    new HtmlWebpackPlugin({
      title: '志愿百科',
      filename: 'index.html',
      template: './src/index.html',
      inject: true,
      minify: {
        removeComments: true,
        collapseBooleanAttributes: true,
        collapseWhitespace: true,
        html5: true,
        minifyJS: true,
        useShortDoctype: true
      }
    }),
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('production')
      },
      '__DEV__': false
    })
  ],
  postcss: function(webpack) {
    return [
      require('postcss-import')({
        addDependencyTo: webpack
      }),
      require('postcss-url')(),
      require('postcss-browser-reporter')(),
      require('postcss-reporter')(),
      require('cssnano')()
    ]
  }
}